var fs = require("fs");
const { log } = require("console");
var tel = [
  "config",
  "controller",
  "router/index.js",
  "router/login.js",
  "router/table.js",
  "dao",
  "view",
  "app.js",
];
var demo = function (mulu) {
  var muluAtr = mulu.split("/");
  muluAtr.forEach(function (value, i) {
    i++;
    if (i == muluAtr.length) {
      if (!!value.match(/\./g)) {
        if (!!fs.existsSync(mulu)) {
          return console.log("文件：" + mulu + "已存在");
        } else {
          if (fs.writeFileSync(mulu, "", "utf8")) {
            return console.log("文件：" + mulu + "创建失败");
          } else {
            return console.log("文件：" + mulu + "创建成功");
          }
        }
      }
    }
    var munstr = muluAtr.slice(0, i).join("/");
    if (!!fs.existsSync(munstr)) {
      return console.log("目录：" + munstr + "已存在");
    } else {
      if (!fs.mkdirSync(munstr)) {
        return console.log("目录：" + munstr + "创建成功");
      } else {
        return console.log("目录：" + munstr + "创建失败");
      }
    }
  });
  if (!!fs.existsSync(mulu) && mulu === "app.js") {
    let expressStart = `
      const express=require('express');
      const logger=require('morgan');
      const bodyParser=require('body-parser');
      const indexRouter=require('./router/index');
      const tableRouter=require('./router/table');
      const loginRouter=require('./router/login');
      const app=express();
      app.use(logger('dev'))
      app.use(express.static(__dirname+'/view'))
      app.use(bodyParser.urlencoded({ extended: false }))
      app.use(bodyParser.json())
      app.use('/',indexRouter)
      app.use('/table',tableRouter)
      app.use('/login',loginRouter)s
      app.listen(8888,()=>console.log('hello express is running'));
    `;
    fs.writeFile("./app.js", expressStart, "utf-8", (err) =>
      console.log(
        "write successful,please muse be download express and logger before node start app.js"
      )
    );
  }
};
tel.forEach(function (value, i) {
  demo(value);
});
